{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "############## PLEASE RUN THIS CELL FIRST! ###################\n",
    "\n",
    "# import everything and define a test runner function\n",
    "from importlib import reload\n",
    "from helper import run\n",
    "import op\n",
    "import script"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 1\n",
    "\n",
    "Write the `op_hash160` function.\n",
    "\n",
    "#### Make [this test](/edit/code-ch06/op.py) pass: `op.py:OpTest:test_op_hash160`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 1\n",
    "\n",
    "reload(op)\n",
    "run(op.OpTest(\"test_op_hash160\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from script import Script\n",
    "z = 0x7c076ff316692a3d7eb3c3bb0f8b1488cf72e1afcd929e29307032997a838a3d\n",
    "sec = bytes.fromhex('04887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34')\n",
    "sig = bytes.fromhex('3045022000eff69ef2b1bd93a66ed5219add4fb51e11a840f404876325a1e8ffe0529a2c022100c7207fee197d27c618aea621406f6bf5ef6fca38681d82b2f06fddbdce6feab601')\n",
    "script_pubkey = Script([sec, 0xac])\n",
    "script_sig = Script([sig])\n",
    "combined_script = script_sig + script_pubkey\n",
    "print(combined_script.evaluate(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 2\n",
    "\n",
    "Write the `op_checksig` function in `op.py`\n",
    "\n",
    "#### Make [this test](/edit/code-ch06/op.py) pass: `op.py:OpTest:test_op_checksig`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 2\n",
    "\n",
    "reload(op)\n",
    "run(op.OpTest(\"test_op_checksig\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 3\n",
    "\n",
    "Create a ScriptSig that can unlock this ScriptPubKey. Note `OP_MUL` multiplies the top two elements of the stack.\n",
    "\n",
    "`767695935687`\n",
    "\n",
    "* `56 = OP_6`\n",
    "* `76 = OP_DUP`\n",
    "* `87 = OP_EQUAL`\n",
    "* `93 = OP_ADD`\n",
    "* `95 = OP_MUL`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 3\n",
    "\n",
    "from script import Script\n",
    "\n",
    "script_pubkey = Script([0x76, 0x76, 0x95, 0x93, 0x56, 0x87])\n",
    "script_sig = Script([])  # FILL THIS IN\n",
    "combined_script = script_sig + script_pubkey\n",
    "print(combined_script.evaluate(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4\n",
    "\n",
    "Figure out what this Script is doing:\n",
    "\n",
    "`6e879169a77ca787`\n",
    "\n",
    "* `69 = OP_VERIFY`\n",
    "* `6e = OP_2DUP`\n",
    "* `7c = OP_SWAP`\n",
    "* `87 = OP_EQUAL`\n",
    "* `91 = OP_NOT`\n",
    "* `a7 = OP_SHA1`\n",
    "\n",
    "Use the `Script.parse` method and look up what various opcodes do at https://en.bitcoin.it/wiki/Script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercise 4\n",
    "\n",
    "from script import Script\n",
    "\n",
    "script_pubkey = Script([0x6e, 0x87, 0x91, 0x69, 0xa7, 0x7c, 0xa7, 0x87])\n",
    "script_sig = Script([])  # FILL THIS IN\n",
    "combined_script = script_sig + script_pubkey\n",
    "print(combined_script.evaluate(0))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
